package kit.docker.convert;

import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class RecordKit {

    /**
     * Model 转 Record
     *
     * @param model 待转换的Model
     * @return Record 转换后的Record
     */
    public static Record recordToModel(Model<?> model) {
        Record record = new Record();
        record.setColumns(model);
        return record;
    }

    /**
     * Record 转 Model （暂未测试）
     *
     * @param record 待转换Record
     * @param clazz  要转成的Model
     * @param <T>    要转成的Model类型
     * @return 转成后的Model
     */
    public static <T> T modelToRecord(Record record, Model clazz) {
        return (T) clazz.put(record);
    }

    public static Record objectToRecord(Object object, List<String> ignoreColumn) {
        Record record = new Record();
        Map<String, Object> map = ConvertKit.objectToMap(object);
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            if (!ignoreColumn.contains(entry.getKey())) {
                record.set(entry.getKey(), entry.getValue());
            }
        }
        return record;
    }

    public static Record objectToRecord(Object object) {
        return objectToRecord(object, new ArrayList<>());
    }

    public static <T> T recordToObject(Record record, Class<T> beanClass) {
        return ConvertKit.mapToObject(record.getColumns(), beanClass);
    }

}
